<?php
	function read_sql_to_db($path,$mode='r'){
		if(!is_file($path))return false;
		$db_sql = fopen($path, $mode);
		$sql = '';
		while(!feof($db_sql)){
			$str = fgets($db_sql);
			$str = trim($str);
			if($str[0] !== '-' && $str[0] !== '/' && !empty($str)){
				$sql .= ' '.$str;
				if(strpos($str,';')!==false){
					$sql = rtrim($sql,';');
					if(JYDB::execute($sql) === false){
						return false;
					}
					$sql = '';
				}
			}
		}
		return true;
	}
	
	function is_ajax(){
		return strtolower($_SERVER['HTTP_X_REQUESTED_WITH'])=='xmlhttprequest';
	}
	
	function set_install(){
		$conpath = APP_PATH.'web/csm-update/flag.php';
		$file = file_get_contents($conpath);
		$file = str_replace("IS_INSTALL',false", "IS_INSTALL',true", $file);
		file_put_contents($conpath,$file);
	}
	
	function set_webtitle($title){
		$conpath = APP_PATH.'tpl/main/index.php';
		$file = file_get_contents($conpath);
		$file = preg_replace('/<title>(.*)<\/title>/i', '<title>'.$title.'-CSM管理系统</title>', $file);
		if(file_put_contents($conpath,$file)){
			return true;
		}
		return false;
	}
	function set_ocx($ftp_name,$ftp_username,$ftp_pwd,$ftp_path){
		$conpath = APP_PATH.'web/csm-update/up-script/ocx.js';
		$target = APP_PATH.'web/js/ocx.js';
		$file = file_get_contents($conpath);
		$src = array('{ftp_ip}','{ftp_user}','{ftp_pwd}','{ftp_path}');
		$m = array($ftp_name,$ftp_username,$ftp_pwd,$ftp_path);
		$file = str_replace($src, $m, $file);
		if(file_put_contents($target,$file)){
			return true;
		}
		return false;
	}
	function set_defaultdb($db_ip,$db_name,$db_user,$db_pwd){
		$conpath = APP_PATH.'web/csm-update/up-script/defaultDB.yml';
		$target = JYAPI_PATH.'config/defaultDB.yml';
		$file = file_get_contents($conpath);
		$src = array('{db_host_ip}','{db_name}','{db_username}','{db_password}');
		$m = array($db_ip,$db_name,$db_user,$db_pwd);
		$file = str_replace($src, $m, $file);
		if(file_put_contents($target,$file)){
			return true;
		}
		return false;
	}
	